package GUIs;

import Configs.Config;
import Controllers.LoaiPhongTroController;
import Controllers.MyTableFormat;
import Controllers.NhaTroController;
import Controllers.PhongTroController;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import models.NhaTro;

public class f_nhanvien_chunhatro_phongtro_them extends javax.swing.JFrame {

    private static f_nhanvien_chunhatro_phongtro_them _instance; //Kiểm tra sự tồn tại của 2 instance trong cùng 1 form
    private final Config conn;
    private PhongTroController phongTroControl;
    private NhaTroController nhaTroControl;
    private LoaiPhongTroController loaiPhongControl;
    public NhaTro nhaTroThem;
    private int rowEdit = -1;
    private String msnv;
    private String cmnd;
    private MyTableFormat myFormat;

    public f_nhanvien_chunhatro_phongtro_them(Config conn) {
        this.conn = conn;
        initComponents();
        this.myFormat = new MyTableFormat();
        this.myFormat.formatTable(this.tbdsphongtro);
        this.myFormat.clearTable(this.tbdsphongtro);
    }

    public static f_nhanvien_chunhatro_phongtro_them getInstance(Config conn, String soGPKD, String msnv, String cmnd) {
        if (_instance == null) {
            _instance = new f_nhanvien_chunhatro_phongtro_them(conn);
        }
        _instance.msnv = msnv;
        _instance.cmnd = cmnd;
        _instance.phongTroControl = new PhongTroController(_instance.conn);
        _instance.nhaTroControl = new NhaTroController(_instance.conn);
        _instance.loaiPhongControl = new LoaiPhongTroController(_instance.conn);
        _instance.nhaTroThem = _instance.nhaTroControl.getNhaTroWithGPKD(soGPKD);
        _instance.loaiPhongControl.setCbbLoaiPhongWithGPDKKD(_instance.cbbloaiphong, soGPKD);
        _instance.clearForm();
        _instance.myFormat.clearTable(_instance.tbdsphongtro);
        return _instance;
    }

    public void createAndShowUI() {
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupmenucontrol = new javax.swing.JPopupMenu();
        menusua = new javax.swing.JMenuItem();
        menuxoa = new javax.swing.JMenuItem();
        pnthemphongtro = new javax.swing.JPanel();
        lbtieude = new javax.swing.JLabel();
        lbmaphong = new javax.swing.JLabel();
        lbloaiphong = new javax.swing.JLabel();
        tbnthem = new javax.swing.JButton();
        btnlammoi = new javax.swing.JButton();
        tfmaphong = new javax.swing.JTextField();
        cbbloaiphong = new javax.swing.JComboBox();
        btnthemloaiphong = new javax.swing.JButton();
        lbsogpdkkd = new javax.swing.JLabel();
        lbsogpdkkd_tt = new javax.swing.JLabel();
        lbtennhatro = new javax.swing.JLabel();
        lbtennhatro_tt = new javax.swing.JLabel();
        spdsphongtro = new javax.swing.JScrollPane();
        tbdsphongtro = new javax.swing.JTable();
        tbnxoaphong = new javax.swing.JButton();

        menusua.setText("Sửa thông tin nhà trọ");
        menusua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menusuaActionPerformed(evt);
            }
        });
        popupmenucontrol.add(menusua);

        menuxoa.setText("Xóa nhà trọ");
        menuxoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuxoaActionPerformed(evt);
            }
        });
        popupmenucontrol.add(menuxoa);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm phòng trọ chủ nhà trọ");
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);

        lbtieude.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lbtieude.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbtieude.setText("Thêm phòng trọ");

        lbmaphong.setText("Mã phòng:");

        lbloaiphong.setText("Loại phòng:");

        tbnthem.setText("Lưu lại");
        tbnthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbnthemActionPerformed(evt);
            }
        });

        btnlammoi.setText("Làm mới");
        btnlammoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlammoiActionPerformed(evt);
            }
        });

        tfmaphong.setEditable(false);

        cbbloaiphong.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "- Chọn loại phòng -" }));

        btnthemloaiphong.setText("...");
        btnthemloaiphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthemloaiphongActionPerformed(evt);
            }
        });

        lbsogpdkkd.setText("Số GPĐKKD:");

        lbsogpdkkd_tt.setText("1122334455");

        lbtennhatro.setText("Tên nhà trọ:");

        lbtennhatro_tt.setText("Tên nhà trọ ABC");

        tbdsphongtro.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Mã phòng", "Loại phòng", "Giá (VND)", "Tình trạng"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbdsphongtro.setComponentPopupMenu(popupmenucontrol);
        tbdsphongtro.getTableHeader().setReorderingAllowed(false);
        spdsphongtro.setViewportView(tbdsphongtro);
        if (tbdsphongtro.getColumnModel().getColumnCount() > 0) {
            tbdsphongtro.getColumnModel().getColumn(0).setResizable(false);
            tbdsphongtro.getColumnModel().getColumn(1).setResizable(false);
            tbdsphongtro.getColumnModel().getColumn(2).setResizable(false);
            tbdsphongtro.getColumnModel().getColumn(3).setResizable(false);
            tbdsphongtro.getColumnModel().getColumn(4).setResizable(false);
        }

        tbnxoaphong.setText("Xóa phòng");
        tbnxoaphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbnxoaphongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnthemphongtroLayout = new javax.swing.GroupLayout(pnthemphongtro);
        pnthemphongtro.setLayout(pnthemphongtroLayout);
        pnthemphongtroLayout.setHorizontalGroup(
            pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnthemphongtroLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tbnthem, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tbnxoaphong, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnlammoi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(pnthemphongtroLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(lbtieude, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(pnthemphongtroLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnthemphongtroLayout.createSequentialGroup()
                        .addComponent(lbsogpdkkd)
                        .addGap(43, 43, 43)
                        .addComponent(lbsogpdkkd_tt))
                    .addComponent(lbloaiphong)
                    .addGroup(pnthemphongtroLayout.createSequentialGroup()
                        .addComponent(lbmaphong)
                        .addGap(43, 43, 43)
                        .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfmaphong, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(pnthemphongtroLayout.createSequentialGroup()
                                .addComponent(cbbloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnthemloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(pnthemphongtroLayout.createSequentialGroup()
                        .addComponent(lbtennhatro)
                        .addGap(43, 43, 43)
                        .addComponent(lbtennhatro_tt))
                    .addComponent(spdsphongtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 10, Short.MAX_VALUE))
        );
        pnthemphongtroLayout.setVerticalGroup(
            pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnthemphongtroLayout.createSequentialGroup()
                .addComponent(lbtieude, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbsogpdkkd)
                    .addComponent(lbsogpdkkd_tt))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbtennhatro)
                    .addComponent(lbtennhatro_tt))
                .addGap(10, 10, 10)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfmaphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbmaphong))
                .addGap(18, 18, 18)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbloaiphong)
                    .addComponent(cbbloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnthemloaiphong))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spdsphongtro, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnlammoi)
                    .addComponent(tbnxoaphong)
                    .addComponent(tbnthem))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnthemphongtro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnthemphongtro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnlammoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlammoiActionPerformed
        this.clearForm();
    }//GEN-LAST:event_btnlammoiActionPerformed

    private void btnthemloaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthemloaiphongActionPerformed
        this.loaiPhongControl.setCbbLoaiPhongWithGPDKKD(this.cbbloaiphong, nhaTroThem.getGPKD());
    }//GEN-LAST:event_btnthemloaiphongActionPerformed

    private void tbnthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbnthemActionPerformed
        if (this.rowEdit == -1) {
            try {
                if (this.phongTroControl.Add(this)) {
                    this.clearForm();
                    f_nhanvien_chunhatro_qlnhatro qlnhatro = f_nhanvien_chunhatro_qlnhatro.getInstance(this.conn, this.msnv, this.cmnd);
                    qlnhatro.refreshdsPhongTro();
                }
            } catch (ParseException ex) {
                Logger.getLogger(f_nhanvien_chunhatro_phongtro_them.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            if (this.phongTroControl.Edit(this)) {
                f_nhanvien_chunhatro_qlnhatro qlnhatro = f_nhanvien_chunhatro_qlnhatro.getInstance(this.conn, this.msnv, this.cmnd);
                qlnhatro.refreshdsPhongTro();
                this.tbdsphongtro.setValueAt(this.cbbloaiphong.getSelectedItem().toString(), this.rowEdit, 2);
                this.rowEdit = -1;
                this.clearForm();
            }
        }
    }//GEN-LAST:event_tbnthemActionPerformed

    private void tbnxoaphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbnxoaphongActionPerformed
        this.deletePhongTro();
    }//GEN-LAST:event_tbnxoaphongActionPerformed

    private void menusuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menusuaActionPerformed
        int row = this.tbdsphongtro.getSelectedRow();
        if (row != -1) {
            String maPhong = this.tbdsphongtro.getValueAt(row, 1).toString();
            String loaiPhong = this.tbdsphongtro.getValueAt(row, 2).toString();
            this.tfmaphong.setText(maPhong);
            this.cbbloaiphong.setSelectedItem(loaiPhong);
            this.rowEdit = row;

        } else {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn đối tượng phòng trọ để sửa!", "Thông báo", JOptionPane.WARNING_MESSAGE);
            this.tbdsphongtro.requestFocus();
        }
    }//GEN-LAST:event_menusuaActionPerformed

    private void menuxoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuxoaActionPerformed
        this.deletePhongTro();
    }//GEN-LAST:event_menuxoaActionPerformed

    private void deletePhongTro() {
        int row = this.tbdsphongtro.getSelectedRow();
        if (row != -1) {
            if (JOptionPane.showConfirmDialog(this, "Xác nhận xóa phòng trọ này?", "Xác nhận", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                String maPhong = this.tbdsphongtro.getValueAt(row, 1).toString();
                if (this.phongTroControl.Delete(maPhong)) {
                    ((DefaultTableModel) this.tbdsphongtro.getModel()).removeRow(row);
                }
                f_nhanvien_chunhatro_qlnhatro qlnhatro = f_nhanvien_chunhatro_qlnhatro.getInstance(this.conn, this.msnv, this.cmnd);
                qlnhatro.refreshdsPhongTro();
            }
        } else {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn đối tượng phòng trọ để xóa!", "Thông báo", JOptionPane.WARNING_MESSAGE);
            this.tbdsphongtro.requestFocus();
        }
    }

    public void clearForm() {
        this.lbsogpdkkd_tt.setText(this.nhaTroThem.getGPKD());
        this.lbtennhatro_tt.setText(this.nhaTroThem.getTennhatro());
        this.tfmaphong.setText(this.phongTroControl.generateMaPhongTro());
        this.cbbloaiphong.setSelectedIndex(0);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlammoi;
    private javax.swing.JButton btnthemloaiphong;
    public javax.swing.JComboBox cbbloaiphong;
    private javax.swing.JLabel lbloaiphong;
    private javax.swing.JLabel lbmaphong;
    private javax.swing.JLabel lbsogpdkkd;
    private javax.swing.JLabel lbsogpdkkd_tt;
    private javax.swing.JLabel lbtennhatro;
    private javax.swing.JLabel lbtennhatro_tt;
    private javax.swing.JLabel lbtieude;
    private javax.swing.JMenuItem menusua;
    private javax.swing.JMenuItem menuxoa;
    private javax.swing.JPanel pnthemphongtro;
    private javax.swing.JPopupMenu popupmenucontrol;
    private javax.swing.JScrollPane spdsphongtro;
    public javax.swing.JTable tbdsphongtro;
    private javax.swing.JButton tbnthem;
    private javax.swing.JButton tbnxoaphong;
    public javax.swing.JTextField tfmaphong;
    // End of variables declaration//GEN-END:variables
}
